package com.jinly.ryder.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jinly.ryder.common.error.SystemErrorType;
import com.jinly.ryder.manage.dao.DeployGroupMapper;
import com.jinly.ryder.manage.dao.entity.DeployGroupDO;
import com.jinly.ryder.manage.service.AppInstanceService;
import com.jinly.ryder.manage.service.DeployGroupService;
import com.jinly.ryder.manage.service.mapping.DeployGroupMapping;
import com.jinly.ryder.manage.service.model.DeployGroup;
import com.jinly.ryder.manage.service.model.DeployStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class DeployGroupServiceImpl implements DeployGroupService {

    private final DeployGroupMapping deployGroupMapping;
    private final DeployGroupMapper deployGroupMapper;
    private final AppInstanceService appInstanceService;

    @Override
    public void changeDeployStatus(Integer deployGroupId, DeployStatus deployStatus, Long applyId) {
        DeployGroup deployGroup = get(deployGroupId);
        if (deployGroup == null) return;
        deployGroup.setDeployStatus(deployStatus);
        deployGroup.setApplyId(applyId);
        save(deployGroup);
    }

    @Override
    public List<DeployGroup> list(String appName) {
        Wrapper<DeployGroupDO> queryWrapper = new QueryWrapper<DeployGroupDO>().lambda()
                .eq(DeployGroupDO::getAppName, appName);
        return deployGroupMapping.toModelList(deployGroupMapper.selectList(queryWrapper));
    }

    @Override
    public void save(DeployGroup deployGroup) {
        if (deployGroup.getProps() == null) {
            deployGroup.setProps(new DeployGroup.DeployGroupProp());
        }
        if (deployGroup.getDeployStatus() == null) {
            deployGroup.setDeployStatus(DeployStatus.NONE);
        }
        if (deployGroup.getId() == null) {
            deployGroupMapper.insert(deployGroupMapping.toEntity(deployGroup));
        } else {
            deployGroupMapper.updateById(deployGroupMapping.toEntity(deployGroup));
        }
    }

    @Override
    public DeployGroup get(Integer deployGroupId) {
        return deployGroupMapping.toModel(deployGroupMapper.selectById(deployGroupId));
    }

    @Override
    public DeployGroup get(String appName, String deployGroupName) {
        Wrapper<DeployGroupDO> queryWrapper = new QueryWrapper<DeployGroupDO>().lambda()
                .eq(DeployGroupDO::getAppName, appName)
                .eq(DeployGroupDO::getName, deployGroupName);
        return deployGroupMapping.toModel(deployGroupMapper.selectOne(queryWrapper));
    }

    @Override
    public void deleteGroup(Integer deployGroupId) {
        DeployGroup deployGroup = get(deployGroupId);
        if (deployGroup == null) return;
        if (appInstanceService.list(deployGroup.getAppName(), deployGroup.getName()).size() != 0) {
            SystemErrorType.BAD_REQUEST.throwError("请先删除分组下实例");
        }
        deployGroupMapper.deleteById(deployGroupId);
    }
}
